#include "RingQueue.hpp"


void* consumer(void* args)  // 消费者
{
    RingQueue<int>* rq = (RingQueue<int>*)args;
    while (true)
    {
        sleep(1);
        // 获取任务
        int x;
        rq->pop(x);

        // 处理任务
        std::cout << "消费: " << x << "[" << pthread_self() << "]" << std::endl;
    }
}

void* productor(void* args)  // 生产者
{
    RingQueue<int>* rq = (RingQueue<int>*)args;
    while (true)
    {
        // 创建任务
        int x = rand() % 100 + 1;
        // 推送任务
        rq->push(x);
        std::cout << "生产: " << x << "[" << pthread_self() << "]" << std::endl;
    }
}

int main()
{
    srand((uint64_t)time(nullptr) ^ 0x582266);
    RingQueue<int>* rq = new RingQueue<int>(10);

    pthread_t c[3], p[2];

    pthread_create(c, nullptr, consumer, (void*)rq);
    pthread_create(c + 1, nullptr, consumer, (void*)rq);
    pthread_create(c + 2, nullptr, consumer, (void*)rq);

    pthread_create(p, nullptr, productor, (void*)rq);
    pthread_create(p + 1, nullptr, productor, (void*)rq);

    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(c[2], nullptr);
    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    return 0;
}
